package net.cardgame.orcalecard;

import java.util.ArrayList;
import java.util.List;

import jp.jma.oraclecard.MyApplication;
import jp.jma.oraclecard.R;
import net.cardgame.oraclecard.common.MySwitch;
import net.cardgame.oraclecard.common.SavedListener;
import net.cardgame.orcalecard.bean.DeckBean;
import net.cardgame.orcalecard.pref.SecurePreferences;
import net.cardgame.orcalecard.utils.ConstantValue;
import android.app.Activity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.ImageView;
import android.widget.TextView;

import com.novoda.imageloader.core.ImageManager;
import com.novoda.imageloader.core.model.ImageTagFactory;

public class ListSpecialAdapter extends ArrayAdapter<DeckBean> implements
		OnCheckedChangeListener {
	ArrayList<DeckBean> listDeck;
	Activity context;
	List<Integer> special;
	SavedListener listener;
	private ImageManager imageManager;
	private ImageTagFactory imageTagFactory;
	boolean flag = true;
	SecurePreferences contentPreferences;

	public ListSpecialAdapter(Activity context, int textViewResourceId,
			ArrayList<DeckBean> objects, List<Integer> listSpecial) {
		super(context, textViewResourceId, objects);
		// TODO Auto-generated constructor stub
		this.listDeck = objects;
		listener = (SavedListener) context;
		this.context = context;
		this.special = listSpecial;
		if (special == null)
			special = new ArrayList<Integer>();
		contentPreferences = new SecurePreferences(context,
				ConstantValue.APP_PREFERENCES,
				ConstantValue.PREFERENCES_SECRET_KEY, true);
		initImageLoader();
	}

	public void setFlag(boolean flag) {
		this.flag = flag;
	}

	static class ViewHolder {
		TextView txtTitle;
		ImageView img_deck;
		MySwitch switch_button;
	}

	public List<Integer> getSpecialList() {
		return special;
	}

	private String getDeckName(DeckBean deckbean) {
		String deckName = "";
		String key = "content_" + deckbean.deckId;
		if (contentPreferences.containsKey(key)) {
			String content = contentPreferences.getString(key);
			String[] arr = content.split("---");
			deckName = arr[0];
			if (deckName.contains("←"))
				deckName = deckName.substring(0, deckName.indexOf("←"));
		}

		return deckName;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		// TODO Auto-generated method stub
		View rowView = convertView;
		if (rowView == null) {
			LayoutInflater inflater = context.getLayoutInflater();
			rowView = inflater.inflate(R.layout.item_listview_setting_special,
					null);
			ViewHolder viewHolder = new ViewHolder();
			viewHolder.txtTitle = (TextView) rowView
					.findViewById(R.id.txt_deck_title);
			viewHolder.img_deck = (ImageView) rowView
					.findViewById(R.id.img_deck);
			viewHolder.switch_button = (MySwitch) rowView
					.findViewById(R.id.switch_on_off);
			rowView.setTag(viewHolder);
		}
		ViewHolder holder = (ViewHolder) rowView.getTag();
		DeckBean deck = listDeck.get(position);
		holder.txtTitle.setText(getDeckName(deck));
		if (special.contains(deck.deckId)) {
			holder.switch_button.setChecked(true);
		} else {
			holder.switch_button.setChecked(false);
		}
		String tt = deck.deckId < 10 ? "0" + deck.deckId : "" + deck.deckId;
		holder.switch_button.setTag(deck.deckId);
		holder.switch_button.setOnCheckedChangeListener(this);
		String strDeck = deck.pathServerDownload + "/c" + tt + "i.png";
		setImageTag(holder.img_deck, strDeck);
		loadImage(holder.img_deck);
		return rowView;
	}

	String convertIdtoString(int id) {
		return id < 10 ? "0" + id : "" + id;
	}

	@Override
	public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
		// TODO Auto-generated method stub
		if (!flag)
			return;
		int deckId = (Integer) buttonView.getTag();
		// Utils.ELog("OnCheckedChanged", "DeckId:" + deckId + "      ischeck:"
		// + isChecked);
		if (isChecked)
			special.add(deckId);
		else if (special.contains((Integer) deckId))
			special.remove((Integer) deckId);
		if (special.isEmpty())
			listener.onSave(false);
		else
			listener.onSave(true);
		// this.notifyDataSetChanged();

	}

	private void initImageLoader() {
		imageManager = MyApplication.getInstance().getImageLoader();
		imageTagFactory = createImageTagFactory();
	}

	private ImageTagFactory createImageTagFactory() {
		ImageTagFactory imageTagFactory = ImageTagFactory.newInstance();
		imageTagFactory.setHeight(248);
		imageTagFactory.setWidth(188);
		imageTagFactory.setDefaultImageResId(R.drawable.transparent_image);
		imageTagFactory.setErrorImageId(R.drawable.no_image);
		imageTagFactory.setSaveThumbnail(true);
		return imageTagFactory;
	}

	private void setImageTag(ImageView view, String url) {
		view.setTag(imageTagFactory.build(url, context));
	}

	private void loadImage(ImageView view) {
		imageManager.getLoader().load(view);
	}
}
